//查询个人产量
var util = require('../../tools/util')
var sql = require('../../tools/mysqlCURD')
const { message: { checkSignature } } = require('../../qcloud')
async function post(ctx, next) {
    // 检查签名，确认是微信发出的请求
    const { signature, timestamp, nonce } = ctx.query
    if (!checkSignature(signature, timestamp, nonce)) ctx.body = 'ERR_WHEN_CHECK_SIGNATURE'

    /**
     * 解析微信发送过来的请求体
     * 可查看微信文档：https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/receive.html#接收消息和事件
     */
    const day=-7//查询几天之前的
    const pageNumber = ctx.request.body.pageNumber
    var openId=ctx.state.$wxInfo.userinfo.openId;
    if(openId){
        const endDate=getdate(new Date(),pageNumber*(day)+1);
        const startDate=getdate(new Date(),(pageNumber+1)*(day)+1);
        const  sqlq="SELECT v.id,v.dingdanId,v.gongxuId,v.number,u.gongxu FROM chanliangTable v LEFT JOIN dingdanTable u ON v.dingdanId=u.name WHERE '"+startDate+"'<v.id AND v.id <='"+endDate+"' AND userId='"+openId+"'ORDER BY v.id desc"
        ctx.body= await sql.query(sqlq)
        ctx.response.status = 200
    }else {
        ctx.state.code = -1
        ctx.body="openid"
    }

}

var getdate=function(date,days){
    var targetday_milliseconds=date.getTime() + 1000*60*60*24*(days);
    date.setTime(targetday_milliseconds);
   return util.DateFormat(date,"yyyyMMdd");
}

module.exports = {
    post
}